package com.yfp.group.controller;

import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.common.security.annotation.InnerAuth;
import com.yfp.group.domain.Enterprise;
import com.yfp.group.domain.dto.EnterpriseAccountDTO;
import com.yfp.group.domain.dto.EnterpriseUserDTO;
import com.yfp.group.domain.dto.GroupRegisterDTO;
import com.yfp.group.domain.vo.EnterpriseAccountVO;
import com.yfp.group.domain.vo.EnterpriseUserVO;
import com.yfp.group.service.EnterpriseService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 
 * @version 1.0
 * @date 2024-08-10 9:53
 */
@RestController
@RequestMapping("/enterpriseUser")
@Tag(name = "企业商户")
@Slf4j
public class EnterpriseController extends BaseController {

    @Resource
    private EnterpriseService enterpriseService;

    @InnerAuth
    @Operation(summary = "h5-注册账号", description = "企业注册账号接口")
    @PostMapping("/register")
    public R<WxPayUnifiedOrderV3Result.JsapiResult> register(@RequestBody GroupRegisterDTO groupRegisterBody) {
        // 注册账号
        WxPayUnifiedOrderV3Result.JsapiResult register = enterpriseService.register(groupRegisterBody);
        return R.ok(register);
    }

    @InnerAuth
    @GetMapping("/info/{userId}")
    @Operation(summary = "h5-获取用户信息", description = "登录后获取用户信息")
    public R<Enterprise> getEnterpriseUserByUserId(@PathVariable Long userId) {
        Enterprise enterprise = enterpriseService.findByUserId(userId);
        return R.ok(enterprise);
    }

    @GetMapping("/getByCurrentUserId")
    @Operation(summary = "h5-我的账号", description = "h5-我的账号-用户信息数据")
    public R<EnterpriseAccountVO> getByCurrentUserId() {
        EnterpriseAccountVO enterpriseAccountVO = enterpriseService.getByCurrentUserId();
        return R.ok(enterpriseAccountVO);
    }

    @PutMapping("/updateAvatar")
    @Operation(summary = "h5-头像修改", description = "h5-我的账号-用户头像修改")
    public R<Void> updateAvatar(@RequestBody EnterpriseAccountDTO enterpriseAccountDTO) {
        enterpriseService.updateAvatar(enterpriseAccountDTO);
        return R.ok();
    }

    @GetMapping("/list")
    @Operation(summary = "获取企业商户列表")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Successful response", content = @Content(mediaType = "application/json", schema = @Schema(implementation = EnterpriseUserVO.class)))})
    public R<TableDataInfo> enterpriseUserList(EnterpriseUserDTO enterpriseUserDTO) {
        startPage();
        List<EnterpriseUserVO> list = enterpriseService.enterpriseUserList(enterpriseUserDTO);
        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }


    @PostMapping("/derive")
    @Operation(summary = "企业商户列表数据导出", description = "数据导出")
    @Log(title = "企业商户列表数据导出", businessType = BusinessType.EXPORT)
    public void deriveEnterpriseUser(HttpServletResponse response, @ModelAttribute EnterpriseUserDTO enterpriseUserDTO) {
        log.info("企业商户列表导出，条件参数:{}", enterpriseUserDTO);
        List<EnterpriseUserVO> list = enterpriseService.enterpriseUserList(enterpriseUserDTO);
        log.info("数据:{}", list);
        ExcelUtil<EnterpriseUserVO> util = new ExcelUtil<>(EnterpriseUserVO.class);
        util.exportExcel(response, list, "企业商户数据");
    }


    /**
     * 根据id获取数据
     *
     * @param id
     * @return
     */
    @GetMapping("/id")
    @Operation(summary = "根据id获取数据")
    public R<Enterprise> selectById(@ApiParam(value = "id") Long id) {
        Enterprise enterprise = enterpriseService.selectById(id);
        return R.ok(enterprise);
    }


    @PutMapping
    @Operation(summary = "修改企业用户")
    public R<Void> updateEnterpriseUser(@RequestBody Enterprise enterprise) {
        log.info("修改企业用户，参数:{}", enterprise);
        enterpriseService.updateEnterpriseUser(enterprise);
        return R.ok();
    }

}
